package Q202

import java.util.HashSet

class Main {

}

class Solution {
    fun isHappy(n: Int): Boolean {
        var n = n
        val record: MutableSet<Int> = HashSet()
        while (n != 1 && !record.contains(n)) {
            record.add(n)
            n = getNextNumber(n)
        }
        return n == 1
    }

    private fun getNextNumber(n: Int): Int {
        var n = n
        var res = 0
        while (n > 0) {
            val temp = n % 10
            res += temp * temp
            n = n / 10
        }
        return res
    }
}


